<?php
namespace Admin\Controller;
use Common\Model\FollowModel;

use Think\Controller;
use Admin\Controller\AdminController; 
use Think\Verify;

class FuturesVoucherController extends AdminController{
	
	/*页面加载*/
	public function index(){
		$voucherTable = D('Futures_voucher');
		/*状态查询*/
		if($_REQUEST['status']){
			if($_REQUEST['status']==1){
				$sql['status']=array('eq',1);
				$status=1;
			}elseif($_REQUEST['status']==2){
				$sql['status']=array('eq',0);
				$status=2;
			}else{
				$status=-1;
			}
		}
		else{
			$status=-1;
		}
		$this->assign('status',$status);
		/*开始时间查询*/
		if ($_REQUEST['begin_time']) {
			$time=strtotime($_REQUEST['begin_time']);
			$start_time=mktime(00,00,00,date('m',$time),date('d',$time),date('Y',$time));
			$end_time=mktime(23,59,59,date('m',$time),date('d',$time),date('Y',$time));
			$sql['begin_time'] = array('between',"$start_time,$end_time");
		}
		/*结束时间查询*/
		if ($_REQUEST['end_time']) {
			$time=strtotime($_REQUEST['end_time']);
			$start_time=mktime(00,00,00,date('m',$time),date('d',$time),date('Y',$time));
			$end_time=mktime(23,59,59,date('m',$time),date('d',$time),date('Y',$time));
			$sql['end_time'] = array('between',"$start_time,$end_time");
		}
		/*创建时间查询*/
		if ($_REQUEST['create_time']) {
			$time=strtotime($_REQUEST['create_time']);
			$start_time=mktime(00,00,00,date('m',$time),date('d',$time),date('Y',$time));
			$end_time=mktime(23,59,59,date('m',$time),date('d',$time),date('Y',$time));
			$sql['create_time'] = array('between',"$start_time,$end_time");
		}
		/*代金卷查询*/
		if($_GET['Cname']){
			$sql['name']=array('like',"%".trim($_REQUEST['Cname'])."%");
		}
		$list = $this->lists($voucherTable,$sql,'id desc');
		//判断是否过期
		$time = time();
		foreach ($list as $v){
			if($time > $v['end_time']){		//修改状态
				M('Futures_voucher')->where('id='.$v[id])->setField('status','0');
			}
		}
		$this->assign('list',$list);
		$this->display();
	}

	/*代金卷添加*/
	public function add(){
		if(IS_POST){
			if(empty($_POST['name'])){		//判断名称是否为空
				$this->error('请编辑代金卷名称');
			}
			if(empty($_POST['money'])){		//判断金额是否为空
				$this->error('请编辑代金卷金额');
			}
			if(empty($_POST['person_limit'])){		//判断每人领取次数是否有限制，默认为1次
				$_POST['person_limit'] = 1;
			}
			if(empty($_POST['begin_time'])){//判断开始时间是否有设置，默认为现在
				$_POST['begin_time'] = time();
			}else{
				$_POST['begin_time'] = strtotime($_POST['begin_time']);
			}
			if(empty($_POST['end_time'])){	//判断是否编辑结束时间
				$this->error('请编辑代金卷结束日期');
			}else{		//判断结束时间是否早于开始时间
				$_POST['end_time'] = strtotime($_POST['end_time']); //时间戳
				if($_POST['end_time']<$_POST['begin_time']){
					$this->error('结束时间早于开始时间');
				}
			}
			//设置状态为正在进行
			$_POST['status'] = 1;
			//设置id
			$id = M('Futures_voucher')->max(id);
			$_POST['id'] = $id+1;
			// 创建时间
			$_POST['create_time'] = time();
			//添加数据
			M('Futures_voucher')->add($_POST);
			$this->success('添加成功',U('FuturesVoucher/index'));
		}else{
			$this->display();
		}
	}
	
	/*代金卷删除*/
	public function del(){
		if(IS_POST){		//选中删除
			foreach($_POST['ids'] as $v){
				//删除数据
				M('Futures_voucher')->where('id='.$v)->delete();
			}
			$this->success('删除成功',U('FuturesVoucher/index'));
		}
		if(IS_GET){			//指定删除
			//删除数据
			M('Futures_voucher')->where($_GET)->delete();
			$this->success('删除成功',U('FuturesVoucher/index'));
		}
	}
	
	/*代金卷编辑*/
	public function updata(){
		if(IS_POST){
			$time = time();
			if(empty($_POST['name'])){		//判断名称是否为空
				$this->error('请编辑代金卷名称');
			}
			if(empty($_POST['money'])){		//判断金额是否为空
				$this->error('请编辑代金卷金额');
			}
			if(empty($_POST['person_limit'])){		//判断每人领取次数是否有限制，默认为1次
				$_POST['person_limit'] = 1;
			}
			if(empty($_POST['begin_time'])){//判断开始时间，默认为现在
				$_POST['begin_time'] = time();
			}else{
				$_POST['begin_time'] = strtotime($_POST['begin_time']);//时间戳
			}
			if(empty($_POST['end_time'])){	//判断是否编辑结束时间
				$this->error('请编辑代金卷结束日期');
			}else{		//判断结束时间是否早于开始时间
				$_POST['end_time'] = strtotime($_POST['end_time']); //时间戳
				if($_POST['end_time']<$_POST['begin_time']){
					$this->error('结束时间早于开始时间');
				}
			}
			if($_POST['status'] == 1){		//判断状态是否正确
				if($time > $_POST['end_time']){
					$this->error('请重新设置结束时间');
				}
			}
			//数据修改
			M('Futures_voucher')->where('id='.$_POST[id])->setField($_POST);
			$this->success('修改成功',U('FuturesVoucher/index'));
		}else{
			$list = M('Futures_voucher')->where($_GET)->find();
			$this->assign('list',$list);
			$this->display();
		}
	}
	
	/*代金卷查看*/
	public function data(){
		/*查询用户*/
		if($_REQUEST['Name']){
			$sql['b.nickname']=array('like',"%".trim($_REQUEST['Name'])."%");
		}
		/*领取时间查询*/
		if ($_REQUEST['create_time']) {
			$time=strtotime($_REQUEST['create_time']);
			$start_time=mktime(00,00,00,date('m',$time),date('d',$time),date('Y',$time));
			$end_time=mktime(23,59,59,date('m',$time),date('d',$time),date('Y',$time));
			$sql['a.create_time'] = array('between',"$start_time,$end_time");
		}
		$sql['voucher_id'] = $_GET['id'];
		/*分页*/
		if(!empty($sql)){
			$count = M('Futures_voucher_records')->table('wp_futures_voucher_records as a')->join('wp_follow as b On a.user_id=b.id')->field('a.id,a.voucher_id,b.nickname,b.mobile,a.money,a.count,a.overdue_time,a.create_time')->where($sql)->count();
		}else{
			$count = M('Futures_voucher_records')->table('wp_futures_voucher_records as a')->join('wp_follow as b On a.user_id=b.id')->count();
		}
		$page = new \Think\Page($count,10);
		if($count>10){
			$page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
		}
		$show = $page->show();
		//关联查询
		$records = M('Futures_voucher_records')->table('wp_futures_voucher_records as a')->where($sql)->join('wp_follow as b On a.user_id=b.id')->field('a.id,a.voucher_id,b.nickname,b.mobile,a.money,a.count,a.overdue_time,a.create_time')->limit($page->firstRow.','.$page->listRows)->select();
		$list = M('Futures_voucher')->where('id='.$_GET[id])->field('name')->find();
		$this->assign('_page', $show? $show: '');
		$this->assign('list',$list);
		$this->assign('records',$records);
		$this->display();
	}
	
	/*统计页面*/
	public function log(){
		/*用户查询*/
		if($_GET['Name']){
			$sql['b.nickname']=array('like',"%".trim($_REQUEST['Name'])."%");
		}
		/*代金卷查询查询*/
		if($_REQUEST['Vname']){
			$sql['d.name']=array('like',"%".trim($_REQUEST['Vname'])."%");
		}
		/*领取时间查询*/
		if ($_REQUEST['create_time']) {
			$time=strtotime($_REQUEST['create_time']);
			$start_time=mktime(00,00,00,date('m',$time),date('d',$time),date('Y',$time));
			$end_time=mktime(23,59,59,date('m',$time),date('d',$time),date('Y',$time));
			$sql['a.create_time'] = array('between',"$start_time,$end_time");
		}
		/*用户记录查询*/
		if(!empty($_GET['Voucher'])){
			$sql['d.name'] = $_GET['Voucher'];
			$sql['b.nickname'] = $_GET['Name'];
			$list = M('Futures_voucher_log')->table('wp_futures_voucher_log as a')->join('wp_follow as b On a.user_id=b.id')->join('wp_futures_voucher as d On a.voucher_id=d.id')->field('a.id,b.nickname,d.name,a.money,a.count,a.type,a.create_time')->where($sql)->order('a.id desc')->select();
			$this->assign('list',$list);
			$this->display('record');die;
		}
		/*分页*/
		if(!empty($sql)){
			$count = M('Futures_voucher_records')->table('wp_futures_voucher_records as a')->join('wp_follow as b On a.user_id=b.id')->join('wp_futures_voucher as d On a.voucher_id=d.id')->where($sql)->count();
		}else{
			$count = M('Futures_voucher_records')->count();
		}
		$page = new \Think\Page($count,10);
		if($count>10){
			$page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
		}
		$show = $page->show();
		/*关联查询*/
		$list = M('Futures_voucher_records')->table('wp_futures_voucher_records as a')->join('wp_follow as b On a.user_id=b.id')->join('wp_futures_voucher as d On a.voucher_id=d.id')->field('a.id,b.nickname,d.name,a.money,a.count,a.create_time')->where($sql)->order('a.id desc')->limit($page->firstRow.','.$page->listRows)->select();
		
		/*赋值*/
		$this->assign('page', $show? $show: '');
		$this->assign('list',$list);
		$this->display();
	}
}